<?php
require_once './gg.php';
$array = array(
    'code' => 0,
    'data' => false,
    'msg' => '缺少必传参数',
);

if (!isset($_REQUEST['username']) || !isset($_REQUEST['password'])) {
    die(json_encode($array));
}

$username = $_REQUEST['username'];
$password = $_REQUEST['password'];

$sql = "select * from userinfo where username = '$username'";

$result = mysqli_query($conn, $sql);

$data = mysqli_fetch_assoc($result);

$array = array(
    'code' => 0,
    'data' => false,
    'msg' => '用户名已存在',
);

if (!$data) {

    $sql = "INSERT INTO userinfo (`username`, `password`) VALUES ('$username','$password')";
    mysqli_query($conn, $sql);

    $rows = mysqli_affected_rows($conn);
    
    if ($rows > 0) {
        $array = array(
            'code' => 1,
            'data' => true,
            'msg' => '注册成功',
        );
    } else {
        $array['msg'] = '系统错误';
    }
}

echo json_encode($array);
